<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jz
 * Date: 2016/8/25
 * Time: 23:11
 */

class User_model extends CI_Model{
    private $error = '';
    public $err_code;
    public $ERROR_NO_USER_PASSWORD = 1;
    public $ERROR_NOT_ACTIVE = 2;

    public function login($login = array())
    {
        $result = $this->db->select('*')->from('user')->where(array('email' => $login['email'], 'password' => $login['password']))->get()->result_array();
        if (!$result || count($result) == 0){
            $this->error = '用户名或密码错';
            $this->err_code = $this->ERROR_NO_USER_PASSWORD;
            return false;
        }
        if ($result[0]['active'] != '1'){
            $this->err_code = $this->ERROR_NOT_ACTIVE;
            $this->error = '账户未激活';
            return false;
        }
        $this->session->set_userdata('email', $login['email']);
        $this->session->set_userdata('uid', $result[0]['id']);
        $this->session->set_userdata('rand', md5(time()));
        $set = array(
            'count' => $result[0]['count'] + 1,
            'rand' => $this->session->userdata('rand')
        );
        $this->db->update('user', $set, array('email' => $login['email']));
        return true;
    }
    
    
    public function get_by_email($email = '')
    {
        $data = array(
            'email' => $email,
        );
        $result = $this->db->select('*')->from('user')->where($data)->get()->result_array();
        if (!$result || count($result) == 0){
            return false;
        }
        return $result[0];
    }
    
    public function update($where = array(), $set=array())
    {
        if (isset($set['password'])){
            $data['password'] = $set['password'];
        }

        if (isset($set['count'])){
            $data['count'] = $set['count'];
        }

        if (isset($set['rand'])){
            $data['rand'] = $set['rand'];
        }

        if (isset($set['rand_register'])){
            $data['rand_register'] = $set['rand_register'];
        }
        if (isset($set['active'])){
            $data['active'] = $set['active'];
        }

        $result = $this->db->update('user', $data, $where);
        if (!$result || $this->db->affected_rows() == 0){
            $this->error = '无法更新';
            return false;
        }
        return true;
    }
    
    public function insert($userinfo)
    {
        if (isset($userinfo['email'])){
            $data['email'] = $userinfo['email']; 
        }

        if (isset($userinfo['password'])){
            $data['password'] = $userinfo['password'];
        }

        if (isset($userinfo['count'])){
            $data['count'] = $userinfo['count'];
        }

        if (isset($userinfo['rand'])){
            $data['rand'] = $userinfo['rand'];
        }
        
        if (isset($userinfo['rand_register'])){
            $data['rand_register'] = $userinfo['rand_register'];
        }
        
        $result = $this->db->select('email')->from('user')->where(array('email' => $userinfo['email']))->get()->result_array();
        if ($result && count($result) > 0){
            return false;
        }

        $result = $this->db->insert('user', $data);
        if ($result){
            if ($this->db->affected_rows() > 0){
                return true;
            }
        }
        return false;
    }

    public function get_rand($email = '')
    {

        $data = array(
            'email' => $email
        );
        $result = $this->db->select('rand')->from('user')->where($data)->get()->result_array();
        if (!$result || count($result) == 0){
            return false;
        }
        return $result[0]['rand'];
    }

    public function display_errors()
    {
        return $this->error;
    }
}